{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# ContextGem: Extracting Aspects from Documents"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# ContextGem: Extracting Aspects from Documents\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import Aspect, Document, DocumentLLM\n",
        "\n",
        "\n",
        "# Sample text content\n",
        "text_content = \"\"\"\n",
        "TechCorp is a leading software development company founded in 2015 with headquarters in San Francisco.\n",
        "The company specializes in cloud-based solutions and has grown to 500 employees across 12 countries.\n",
        "Their flagship product, CloudManager Pro, serves over 10,000 enterprise clients worldwide.\n",
        "TechCorp reported $50 million in revenue for 2023, representing a 25% growth from the previous year.\n",
        "The company is known for its innovative AI-powered analytics platform and excellent customer support.\n",
        "They recently expanded into the European market and plan to launch three new products in 2024.\n",
        "\"\"\"\n",
        "\n",
        "# Create a Document object from text\n",
        "doc = Document(raw_text=text_content)\n",
        "\n",
        "# Define aspects to extract from the document\n",
        "doc.aspects = [\n",
        "    Aspect(\n",
        "        name=\"Company Overview\",\n",
        "        description=\"Basic information about the company, founding, location, and size\",\n",
        "    ),\n",
        "    Aspect(\n",
        "        name=\"Financial Performance\",\n",
        "        description=\"Revenue, growth metrics, and financial indicators\",\n",
        "    ),\n",
        "    Aspect(\n",
        "        name=\"Products and Services\",\n",
        "        description=\"Information about the company's products, services, and offerings\",\n",
        "    ),\n",
        "]\n",
        "\n",
        "# Configure DocumentLLM with your API parameters\n",
        "llm = DocumentLLM(\n",
        "    model=\"azure/gpt-4.1-mini\",\n",
        "    api_key=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_KEY\"),\n",
        "    api_version=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_VERSION\"),\n",
        "    api_base=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_BASE\"),\n",
        ")\n",
        "\n",
        "# Extract aspects from the document\n",
        "extracted_aspects = llm.extract_aspects_from_document(doc)\n",
        "\n",
        "# Access extracted aspect information\n",
        "for aspect in extracted_aspects:\n",
        "    print(f\"Aspect: {aspect.name}\")\n",
        "    print(f\"Extracted items: {[item.value for item in aspect.extracted_items]}\")\n",
        "    print(\"---\")\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}